﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;


namespace Miao.UI
{
    /// <summary>
    /// 表示UI面板，与Wins不同的是，面板基于MonoBehaviour,面板重新可视化。
    /// 本身也可以改为继承UIBehaviour相关
    /// </summary>
    public abstract class UIPanel : MonoBehaviour
    {
        public RectTransform rectTransform { get; internal set; }

        /// <summary>
        /// 显示面板
        /// </summary>
        /// <returns></returns>
        virtual public void Show()
        {
            gameObject.SetActive(true);
            OnShow();
        }
        /// <summary>
        /// 隐藏面板
        /// </summary>
        /// <returns></returns>
        virtual public void Hide()
        {
            OnHide();
            gameObject.SetActive(false);
        }
        /// <summary>
        /// 关闭面板,删除界面
        /// </summary>
        virtual public void Close()
        {
            OnClose();
            UIManager.ClosePanel(this);
        }

        /// <summary>
        /// 当面板被创建时
        /// </summary>
        public virtual Task OnCreate() 
        {
            return Task.CompletedTask;
        }
        /// <summary>
        /// 当面板打开时
        /// </summary>

        public virtual void OnShow() { }
        /// <summary>
        /// 当面板隐藏时
        /// </summary>
        public virtual void OnHide() { }
        /// <summary>
        /// 当面板关闭时
        /// </summary>
        public virtual void OnClose() { }

        public static implicit operator bool(UIPanel self)
        {
            return self != null;
        }
#if UNITY_EDITOR
        [ContextMenu("快速绑定同名引用")]
        public void FindBind()
        {
            GameObject[] allTransforms = GetComponentsInChildren<Transform>().Select(t => t.gameObject).ToArray();

            // 获取当前脚本的Type
            Type scriptType = this.GetType();
            Dictionary<string, FieldInfo> rlSave = new Dictionary<string, FieldInfo>();

            foreach (FieldInfo field in scriptType.GetFields())
            {
                rlSave.Add(field.Name, field);
            }

            for (int i = 0; i < allTransforms.Length; i++)
            {
                if (rlSave.ContainsKey(allTransforms[i].name))
                {
                    FieldInfo field = rlSave[allTransforms[i].name];
                    rlSave[allTransforms[i].name].SetValue(this, allTransforms[i].GetComponent(field.FieldType));
                }
            }

            UnityEditor.EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();

            // 遍历所有子对象
            //    foreach (Transform child in allTransforms)
            //{

            //        // 使用反射获取当前脚本中声明的变量

            //        {
            //            // 检查变量名是否与组件类型名称相同
            //            //if (field.Name == componentName)
            //            //{
            //            //    // 赋值
            //            //    field.SetValue(this, component);
            //            //}
            //        }
            //}
        }
#endif
    }
}